package com.wytec.xywzxw.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wytec.xywzxw.bean.AccessLog;
import com.wytec.xywzxw.bean.commonBean.PageSizeBean;
import com.wytec.xywzxw.bean.User;
import com.wytec.xywzxw.constant.MyConstant;
import com.wytec.xywzxw.mapper.AccessLogMapper;
import com.wytec.xywzxw.service.AccessLogService;
import com.wytec.xywzxw.service.UserService;
import com.wytec.xywzxw.utils.CommonUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccessLogServiceImpl implements AccessLogService {

    @Autowired
    UserService userService;
    @Autowired
    private AccessLogMapper accessLogMapper;

    @Override
    public void insertAccessLog(HttpServletRequest request,String name, String responseBody) {
        if (request.getRequestURI().contains("/log/") || request.getRequestURI().contains("/test/")) {
            return;
        }
        String ip = CommonUtils.getIpAddr();
        String method = request.getMethod();
        String uri = request.getRequestURI();
        String agent = request.getHeader("User-Agent");
        AccessLog accessLog = new AccessLog();
        accessLog.setIp(ip);
        accessLog.setMethod(method);
        accessLog.setPath(uri);
        accessLog.setResBody(responseBody);
        accessLog.setAgent(agent);
        accessLog.setHost(request.getRemoteHost());
        accessLog.setName(name);
        String token = request.getHeader(MyConstant.TOKEN);
        if (token != null) {
            User userInfo = userService.getUserInfo(token);
            if (userInfo != null) {
                accessLog.setUserId(userInfo.getId());
                accessLog.setAccount(userInfo.getUsername());
            }
        }


        Enumeration<String> pNames = request.getParameterNames();
        Map<String, Object> map = new HashMap<>();
        while (pNames.hasMoreElements()) {
            String key = pNames.nextElement();
            String value = request.getParameter(key);
            map.put(key, value);
        }
        accessLog.setReqBody(JSON.toJSONString(map));

        Map<String, String> regions = CommonUtils.getIpRegion(ip);
        String region = regions.get("region");
        String city = regions.get("city");
        String province = regions.get("province");

        accessLog.setRegion(region);
        accessLog.setCity(city);
        accessLog.setProvince(province);
        accessLogMapper.insert(accessLog);
    }

    @Override
    public AccessLog getAccessLog(int id) {
        return accessLogMapper.selectById(id);
    }

    @Override
    public List<AccessLog> getAccessLogList(PageSizeBean qry) {
        QueryWrapper<AccessLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        PageHelper.startPage(qry.getPage(), qry.getSize());
        List<AccessLog> accessLogs = accessLogMapper.selectList(queryWrapper);
        return accessLogs;
    }


}
